package com.shoeHouse.user.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shoeHouse.user.model.UserInfo;
import com.shoeHouse.user.service.UserInfoService;
import com.shoeHouse.user.service.UserInfoServiceImpl;
import com.shoeHouse.util.ConfigProperty;
import com.shoeHouse.util.DataGrid;
import com.shoeHouse.util.FileUploadUtil;
import com.shoeHouse.util.IdWork;
import com.shoeHouse.util.PageHelper;
import com.shoeHouse.util.ResultInfo;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/userInfo")
public class UserInfoAction {
	@Autowired 
	private UserInfoService userInfoService;
	
	@RequestMapping("/loginPage")
	public String loginPage(){
		return "/pages/login.jsp";
		
		
	}
	@RequestMapping("/main")
	public String main(HttpSession session,Map<String, Object> map){
		
		UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
		map.put("userInfo",userInfo);
		return "/layout.jsp";
	}
	@ResponseBody
	@RequestMapping("/login")
	public ResultInfo login(String username,String password,HttpSession session){
		UserInfo userInfo=new UserInfo();
		userInfo.setUsername(username);
		//userInfo.setPassword(password);
		UserInfo userInfo2=userInfoService.getSingle(userInfo);
	     ResultInfo info=new ResultInfo();
		if(null!=userInfo2.getUsername()&&username.equals(userInfo2.getUsername())){
			if(null!=userInfo2.getPassword()&&password.equals(userInfo2.getPassword())){
				System.out.println(userInfo2.getRoleId());
				
				if(!userInfo2.getRoleId().equals("5")){
				  session.setAttribute("userInfo",userInfo2);
                   info.setSuccess(0);
                   info.setMsg("登录成功");
                   return info;
				}else{
					info.setSuccess(1);
	                info.setMsg("权限不够");
				}
			}
			else{
				info.setMsg("密码错误");
			    info.setSuccess(1);
			}
		}
		else{
			info.setMsg("用户名错误");
		    info.setSuccess(1);
		}
		return info;
	}
	@RequestMapping(value = "/index")
	public String index(HttpSession session,HttpServletRequest request,Map<String, Object> map) {
		UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
       if(userInfo.getRoleId().equals("1")||userInfo.getRoleId().equals("2")||userInfo.getRoleId().equals("3")){
    	   
    	   map.put("role","role");
       }
		return "/pages/user/list.jsp";
	}
//	@ResponseBody
//	@RequestMapping(value = "/dataGrid")
//	public DataGrid dataGrid(@ModelAttribute OrderInfo o, PageHelper ph,String begin_time,String end_time,HttpServletRequest request){
//
//	
//
//		return null;
//	} 
	@ResponseBody
	@RequestMapping("/view")
	public DataGrid view(String departmentId,HttpServletRequest request,HttpServletResponse response,Integer page,Integer rows){
		//http://localhost:8080/shoeHouse/userInfo/view?page=1&rows=10
		//page表示当前页，rows表示一次查询的条数
		PageHelper pageHelper=new PageHelper();
		pageHelper.setCurrentPage(page);
		pageHelper.setPageSize(rows);
		//查询总数
		   DataGrid dataGrid=new DataGrid();
		   UserInfo userInfo=new UserInfo();
		   userInfo.setSex(request.getParameter("sex"));
		   if(request.getParameter("name")!=null){
			   try {
//				   System.out.println();
//				  System.out.println( new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8"));
				userInfo.setName(URLDecoder.decode(request.getParameter("name"),"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		   }
		  
		   userInfo.setDepartmentId(departmentId);
		List<UserInfo> list=userInfoService.getAll(request,userInfo,pageHelper);
		int count=userInfoService.count();
		dataGrid.setTotal(count);
		dataGrid.setRows(list);

		return dataGrid;
			
			
			
		}
		
	
		
		
	
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map,HttpSession session) {
		UserInfo userInfos=(UserInfo) session.getAttribute("userInfo");
if(userInfos.getRoleId().equals("1")||userInfos.getRoleId().equals("2")||userInfos.getRoleId().equals("3")){
			
			map.put("role","role");
		}
		
		 map.put("method","addSave");
		return "/pages/user/save.jsp";
		
	}

	@ResponseBody
	@RequestMapping(value ="/addSave")
	public ResultInfo addSave(@ModelAttribute("userInfo") UserInfo  userInfo,HttpServletRequest request,@RequestParam(value = "uploadFile", required = false) MultipartFile file){
		// UserInfo userInfo=new UserInfo();
		System.out.println(file != null);
		if(file != null){
			try {
				String icon_url = FileUploadUtil.saveFile(file, request,"photo");
				System.out.println(icon_url);
				userInfo.setPicture(icon_url);
			} catch (Exception e) {
				
			}
		}
		
		System.out.println(userInfo.getName()+"#####"+request.getParameter("name"));
	       Date date=new Date();
	     userInfo.setId(IdWork.nextId());
	     userInfo.setEndLoginTime(date);
	     userInfo.setGmtCreate(date);
	     userInfo.setGmtModified(date);
	     userInfo.setLoginIp(request.getRemoteAddr());
	     userInfo.setStatus(Byte.parseByte("0"));
	     if(userInfo.getRoleId()==null){
	    	 
	    	  userInfo.setRoleId("4");
	     }
	   
	     userInfoService.save(userInfo);
	     ResultInfo result=new ResultInfo();
	     result.setSuccess(0);
		return result;
	}
	
	
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
//		map.put("saveUrl", "/manage/apartment/editSave");
		UserInfo userInfo=new UserInfo();
		userInfo.setId(id);
		UserInfo userInfos=userInfoService.getSingle(userInfo);
		if(userInfos.getRoleId().equals("1")||userInfos.getRoleId().equals("2")||userInfos.getRoleId().equals("3")){
			
			map.put("role","role");
		}
		map.put("model",userInfos);
		 map.put("method","editSave");
	    System.out.println("user");
		return "/pages/user/save.jsp";
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ResultInfo editSave(UserInfo userInfo,HttpServletRequest request,@RequestParam(value = "uploadFile", required = false) MultipartFile file) {
		if(file != null){
			try {
				String icon_url = FileUploadUtil.saveFile(file, request,"photo");
				System.out.println(icon_url);
				userInfo.setPicture(icon_url);
			} catch (Exception e) {
				
			}
		}
		
		userInfo.setGmtModified(new Date());
		userInfoService.update(userInfo);
		ResultInfo result=new ResultInfo();
		result.setSuccess(0);
		System.out.println("hahhahahazh");
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/delete")
	public String delete(String ids) {
		
		return null;
	}

	
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ResultInfo remove(String ids) {
		ResultInfo info=new ResultInfo();
       userInfoService.removeAll(ids);
		info.setMsg("正常");
		info.setSuccess(0);

		return info;
	}
	@ResponseBody
	@RequestMapping(value = "/updateStatus")
	public String updateStatus(String ids) {
		
		
		return null;
	}
}

	
	
	

